<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\ProductNews
 *
 * @property int $id
 * @property int|null $uid 发布用户的ID
 * @property string|null $title 产销标题
 * @property string $image
 * @property int $type
 * @property string|null $province 产销的详细地址
 * @property string|null $city 产销的详细地址
 * @property string|null $district 产销的详细地址
 * @property string|null $street 产销的详细地址
 * @property float|null $quantity 产量
 * @property int|null $state 信息状态 1 待审核 2正常 3关闭
 * @property string|null $unit 单位
 * @property float|null $sold 已销产量
 * @property string|null $price 单价
 * @property string|null $phone 联系电话
 * @property string|null $contact  联系人
 * @property string|null $content 产品描述
 * @property int $views
 * @property mixed $created_at
 * @property mixed $updated_at
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ProductImages[] $images
 * @property-read \App\Models\User|null $user
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews query()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereContact($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereContent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereDistrict($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereImage($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews wherePhone($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews wherePrice($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereQuantity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereSold($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereState($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereStreet($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereTitle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereUnit($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ProductNews whereViews($value)
 * @mixin \Eloquent
 */
class ProductNews extends Model
{
    protected $table = 'product_news';
    protected $primaryKey = 'id';
    protected $guarded = [];
    protected $dateFormat = 'U';
    protected $casts = [
        'created_at'=>'datetime:Y-m-d H:i:s',
        'updated_at'=>'datetime:Y-m-d H:i:s',
    ];

    public static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::deleted(function ($model){
            $model->images()->delete();
        });
    }

    /**
     * @param $value
     * @return string
     */
    public function getImageAttribute($value){
        return image_url($value);
    }

    /**
     * @param $value
     */
    public function setImageAttribute($value){
        $this->attributes['image'] = str_replace(material_url(),'',$value);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function images()
    {
        return $this->hasMany(ProductImages::class, 'product_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'uid', 'uid');
    }
}
